import util from '@/libs/util';

export default {
    name: 'exportable-table',
    data () {
        return {
            data: [],
            formData: {},
            orgName: '',
            isDisabled: false,
            butName: '获取验证码',
            isCan: false,
            tabname: this.$route.params.tabname
        };
    },
    mounted () {

    },
    methods: {
        logon () {
            if (!(this.check())) return;
            let formData = {};
            for (let key in this.formData) {
                formData[key] = this.formData[key];
            }
            console.log(formData);
            if (this.tabname === 'organize') {
                util.ajax.post('/busine/organize/save', util.toFormData(formData)).then(res => {
                    if (res.data.message === 'SUCCESS') {
                        this.$Message.success('保存成功，请等待审核！');
                        this.formData = {};
                    } else {
                        this.$Message.warning(res.data.message || '未知错误！');
                    }
                });
            } else if (this.tabname === 'member') {
                util.ajax.post('/busine/member/save', util.toFormData(formData)).then(res => {
                    if (res.data.message === 'SUCCESS') {
                        this.$Message.success('保存成功！');
                        this.formData = {};
                    } else {
                        this.$Message.warning(res.data.message || '未知错误！');
                    }
                });
            }
        },
        back () {
            this.$router.go(-1);
        },
        getCode () {
            let count = 60;
            let interval = setInterval(() => {
                if (count === 0) {
                    clearInterval(interval);
                    this.butName = '发送验证码';
                    this.isDisabled = false;
                } else {
                    count--;
                    this.butName = count + '秒后重发';
                    this.isDisabled = true;
                }
            }, 1000);
        },
        check () {
            let myreg = /^(((17[0-9]{1})|(13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
            let passwords = /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{6,18}$/;
            let formData = this.formData;
            let boo = true;
            let message = '';
            if (this.tabname === 'organize') {
                if (formData.orgName === '' || !(formData.orgName)) {
                    message = '请输入单位名称！';
                    boo = false;
                } else if (formData.orgContacts === '' || !(formData.orgContacts)) {
                    message = '请输入联系人！';
                    boo = false;
                } else if (formData.orgPhone === '' || !(formData.orgPhone)) {
                    message = '请输入联系电话！';
                    boo = false;
                } else if (!myreg.test(formData.orgPhone)) {
                    message = '请输入有效联系电话！';
                    boo = false;
                } else if (formData.userName === '' || !(formData.userName)) {
                    message = '请输入用户名！';
                    boo = false;
                } else if (formData.userPassword === '' || !(formData.userPassword)) {
                    message = '请输入密码！';
                    boo = false;
                } else if (!passwords.test(formData.userPassword)) {
                    message = '密码必须包含至少一个字母和6-18个数字组成';
                    boo = false;
                } else if (formData.userPassword !== formData.checkPassword) {
                    message = '两次输入密码不一致！';
                    boo = false;
                }
            } else if (this.tabname === 'member') {
                if (formData.username === '' || !(formData.username)) {
                    message = '请输入用户名！';
                    boo = false;
                } else if (formData.password === '' || !(formData.password)) {
                    message = '请输入密码！';
                    boo = false;
                } else if (!passwords.test(formData.password)) {
                    message = '密码必须包含至少一个字母和6-18个数字组成';
                    boo = false;
                } else if (formData.password !== formData.checkPassword) {
                    message = '两次输入密码不一致！';
                    boo = false;
                } else if (formData.realname === '' || !(formData.realname)) {
                    message = '请输入姓名！';
                    boo = false;
                } else if (formData.idcard === '' || !(formData.idcard)) {
                    message = '请输入身份证！';
                    boo = false;
                } else if (formData.phone === '' || !(formData.phone)) {
                    message = '请输入联系电话！';
                    boo = false;
                } else if (!myreg.test(formData.phone)) {
                    message = '请输入有效联系电话！';
                    boo = false;
                }
            }

            if (!boo) {
                this.$Modal.error({
                    title: '警告',
                    content: message
                });
            }
            return boo;
        },
        checkHave (that) {
            let formData = this.formData;
            let controller = '';
            this.isCan = false;
            if (this.tabname === 'organize') {
                controller = '/busine/organize/checkHave';
            } else if (this.tabname === 'member') {
                controller = '/busine/member/checkHave';
            }
            util.ajax.post(controller, util.toFormData(formData)).then(res => {
                if (res.data.message !== 'SUCCESS') {
                    this.$Modal.error({
                        title: '警告',
                        content: res.data.message
                    });
                    this.isCan = true;
                }
            });
        }
    }
};